package com.deep.order.config;

import com.deep.order.annotation.ApiController;
import com.deep.order.annotation.ApiRestController;
import com.deep.order.interceptor.LoginInterceptor;
import com.deep.order.model.properties.ApiPathProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * web配置类
 *
 * @author Deep
 * @date 2022/2/14
 */
@Configuration
public class OrderWebConfig implements WebMvcConfigurer {

    @Autowired
    private ApiPathProperties apiPathProperties;

    /**
     * 添加视图控制器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/order/confirm.html").setViewName("confirm");
        registry.addViewController("/order/detail.html").setViewName("detail");
        registry.addViewController("/order/list.html").setViewName("list");
        registry.addViewController("/order/pay.html").setViewName("pay");
    }

    /**
     * 添加拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**");  // 注册拦截器
    }



    /**
     * 请求路径统一管理
     */
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer
                .addPathPrefix(apiPathProperties.getGlobalPrefix(), c -> c.isAnnotationPresent(ApiRestController.class))
                .addPathPrefix(apiPathProperties.getGlobalPrefix(), c -> c.isAnnotationPresent(ApiController.class));
    }
}
